//
//  JMpicReusableView.m
//  仿生物无忧
//
//  Created by 彭继宗 on 15/12/5.
//  Copyright © 2015年 彭继宗. All rights reserved.
//

#import "JMpicReusableView.h"

#import "JMFocusVideos.h"

@interface JMpicReusableView()

//广告数目
#define PageControlNum 5

#define sWidth self.ScrollView.frame.size.width
#define sHeight self.ScrollView.frame.size.height

//广告滚动view
@property (weak, nonatomic) IBOutlet UIScrollView *ScrollView;
//pageControl宽度约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pageControlWidth;
//自制pageControl
@property (weak, nonatomic) IBOutlet UIView *pageControl;

//pageControl标记
@property (nonatomic, strong) UIImageView *moveMark;
//pageControl占位
@property (nonatomic, strong) UIView *pageBackgroundView;
//pageControl占位数组
@property (nonatomic, strong) NSMutableArray *marking;
//定时器
@property (nonatomic, strong) NSTimer *timer;
//页码
@property (nonatomic , assign) int index;

@end


@implementation JMpicReusableView


//从Xib中加载调用
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //设置代理
    self.ScrollView.delegate = self;
    
    self.ScrollView.transform = CGAffineTransformMakeScale(0, 0);
    
    //加载pageControl
    [self loadPageControlView];
    

    
    //一页页滚动
    self.ScrollView.pagingEnabled = YES;
    
    //隐藏滚动条
    self.ScrollView.showsHorizontalScrollIndicator = NO;
    
    //设置一开始偏移量
    self.ScrollView.contentOffset = CGPointMake(sWidth , 0);
    
    //设置timer
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(next) userInfo:nil repeats:YES];
    
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

#pragma mark- 创建广告图片
/**
 *  创建广告图片
 */
- (void)getImage
{

    dispatch_async(dispatch_get_main_queue(), ^{
        //设置一开始偏移量
        self.ScrollView.contentOffset = CGPointMake(sWidth, 0);
        //设置滚动范围
        
        self.ScrollView.contentSize = CGSizeMake((PageControlNum+2) * sWidth, 0);
    });
    
    // 动画
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:0 animations:^{
        //恢复动画
        self.ScrollView.transform = CGAffineTransformIdentity;
    } completion:nil];
    
    for (int i = 0 ; i < PageControlNum + 2; i++)
    {
        
        CGFloat imgX = i * sWidth;
        CGFloat imgY = 0;
        CGFloat imgW = sWidth;
        CGFloat imgH = sHeight;
        UIImageView *imgView = [[UIImageView alloc] init];
        
        if (i == 0)
        {
            //第0张 显示广告最后一张
            imgX = 0;
            [imgView sd_setImageWithURL:[NSURL URLWithString:self.picArray[PageControlNum-1]]];
        }
        else if (i == PageControlNum +1)
        {
            //第n+1张,显示广告第一张
            imgX = (PageControlNum+1)*sWidth;
            [imgView sd_setImageWithURL:[NSURL URLWithString:self.picArray[0]]];
        }
        else
        {
            //正常显示广告
            [imgView sd_setImageWithURL:[NSURL URLWithString:self.picArray[i-1]]];
            
        }
        imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        //添加子控件
        [self.ScrollView addSubview:imgView];
    }
}


/**
 *  执行下一页的方法
 */
- (void)next;
{
    self.index++;
    
    [self.ScrollView setContentOffset:CGPointMake(self.index * sWidth, 0) animated:YES];
}


/**
 *  创建pageControl
 */
- (void)loadPageControlView{
    //
    [self.pageControl addSubview:self.pageBackgroundView];
    
    
    //循环创建占位point
    for (int i = 0; i<PageControlNum; i++)
    {
        UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.pageBackgroundView addSubview:imgv];
        [self.marking addObject:imgv];
    }
    
    //创建当前point
    [self.pageBackgroundView addSubview:self.moveMark];
    
    //设置pageControl坐标
    [self reloadPageViewSize];
}


/**
 * 加载pageControl坐标
 */
- (void)reloadPageViewSize{
    
    CGSize pageSize_def = CGSizeMake(12, 12);
    CGSize pageSize_cur = CGSizeMake(12, 12);
    
    //设置自制pageControl
    self.pageControlWidth.constant = PageControlNum * 12;
    
    //占位point
    for (int i = 0; i < self.marking.count ; i++)
    {
        UIImageView *imgv = (UIImageView *)self.marking[i];
        //坐标
        imgv.frame = CGRectMake(i * pageSize_def.width, 0, pageSize_def.width * 0.5, pageSize_def.height * 0.5);
        //图标
        imgv.image = [UIImage imageNamed:@"tb1"];
    }
    
    //当前point
    //坐标
    self.moveMark.frame = CGRectMake(0, 0, pageSize_cur.width * 0.5, pageSize_cur.height * 0.5);
    //图标
    self.moveMark.image = [UIImage imageNamed:@"tb2"];
}

/**
 *  设置数组时刷新图片
 *
 */
- (void)setPicArray:(NSArray *)picArray
{
    _picArray = picArray;
    
    [self getImage];
    
}

#pragma mark- 懒加载
/**
 *  懒加载
 */

- (NSMutableArray *)marking
{
    if (_marking == nil)
    {
        _marking = [NSMutableArray array];
    }
    return _marking;
}

- (UIView *)pageBackgroundView
{
    if (_pageBackgroundView == nil)
    {
        _pageBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _pageBackgroundView;
}

- (UIImageView *)moveMark
{
    if (_moveMark == nil)
    {
        _moveMark = [[UIImageView alloc] initWithFrame:CGRectZero];
    }
    return _moveMark;
}



#pragma mark- 代理

/**
 *  拖动画减速时的判断
 *
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}

/**
 *  拖拽减速时的判断
 *
 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取偏移值
    CGFloat offset = self.ScrollView.contentOffset.x;
    //当前页
    int page = (offset + sWidth/2) /sWidth;
    //如果是N+1页
    if (page == PageControlNum + 1)
    {
        //瞬间跳转第1页
        [self.ScrollView setContentOffset:CGPointMake(sWidth, 0) animated:NO];
    }
    //如果是第0页
    else if (page == 0)
    {
        //瞬间跳转最后一页
        [self.ScrollView setContentOffset:CGPointMake(PageControlNum * sWidth, 0) animated:NO];
    }
}


/**
 *  滚动时判断页码
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取偏移值
    CGFloat offset = self.ScrollView.contentOffset.x - sWidth;
    //页码
    self.index = (offset + sWidth/2) /sWidth;
    //计算比例
    float size = offset / (sWidth * PageControlNum);
    //计算相应得point坐标
    CGFloat markX = size * self.pageControl.frame.size.width;
    
    
    if (offset > (PageControlNum - 1) * sWidth)
    {
        self.moveMark.frame = CGRectMake(sWidth, 0, 6, 6);
    }
    else if (offset < 0)
    {
        self.moveMark.frame = CGRectMake(PageControlNum * sWidth, 0, 6, 6);
    }
    else
    {
        self.moveMark.frame = CGRectMake(markX, 0, 6, 6);
    }
}

/**
 *  拖拽广告时停止timer
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    
    self.timer = nil;
}

/**
 *  结束拖拽时重新创建timer
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(next) userInfo:nil repeats:YES];
    
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}


@end
